package modulesAPI.v2.composite;

import java.util.GregorianCalendar;

import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import contract.EmployeeContract;

import person.Customer;
import person.Employee.Role;
import person.Person;
import model.Model;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModTextField;

public class MUser extends Module<Object[]> {
	//posizione 0: Customer o Employee, posizione 1: userName, posizione 2: password
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un utente
	 * */
	public MUser(Module<?> ownerModule, Object[] content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, null, tag, description, viewType,options);
		GregorianCalendar regDate=new GregorianCalendar();
		regDate.add(GregorianCalendar.YEAR, -18);
		if (content==null)
			if (super.isOptionEnabled(EMPLOYEE))
				this.setContent(new Object[]{new EmployeeContract(new GregorianCalendar(), new GregorianCalendar(), "", Role.HUMAN_RESOURCES_RESPONSABLE, new Person(Model.NO_ID, "", "", "", "", "", "", "", regDate), 0.0, false),"",""});
			else
				this.setContent(new Object[]{new Customer("", "", "", "", "", "", "", Customer.Type.PERSON, "",new GregorianCalendar()),"",""});
		createModules();
		if (this.isOptionEnabled(EMPLOYEE)) 
			this.addModules(userName, password, employee);
		else
			this.addModules(userName, password, customer);
	}
	/**
	 * Crea un nuovo modulo per l'inserimento/rappresentazione di un utente
	 * */
	public MUser(Module<?> ownerModule, String tag, String description, ViewType viewType,int ...options) {
			this(ownerModule, null, tag, description, viewType,options);
	}
	private void createModules(){
		this.createStandardView();	
	}
	private void createStandardView(){
		this.userName=new ModTextField(this, (String)this.getContent()[1], "USER_NAME", "USER_NAME_DESC", this.getViewType() );
		this.password = new ModTextField(this, (String)this.getContent()[2], "LABEL_PASSWORD", "PASSWORD_DESC", this.getViewType(),ModTextField.PASSWORD);
		if (isOptionEnabled(EMPLOYEE)) this.employee=new MEmployeeContract(this, (EmployeeContract)this.getContent()[0], "EMPLOYEE_DETAILS", "EMPLOYEE_DETAILS_DESC", this.getViewType());
		else this.customer=new MCustomer(this, (Customer)this.getContent()[0], "CUSTOMER_DETAILS", "CUSTOMER_DETAILS_DESC", this.getViewType());
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(((Customer)this.getContent()[0]).getName()+"  "+((Customer)this.getContent()[0]).getSurname(), 
				CWords.getWord(((Customer)this.getContent()[0]).getType().name()), isSelected);
	}
	protected void beforeFireChange(Module<?> mod){
		if(isOptionEnabled(EMPLOYEE))
			this.setContent(new Object[]{employee.getContent(),userName.getContent(),password.getContent()});
		else
			this.setContent(new Object[]{customer.getContent(),userName.getContent(),password.getContent()});
	}
	@Override
	protected void afterFireChange(Module<?> mod) {}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	public int getRegistrationMethod(){
		if (isOptionEnabled(EMPLOYEE))return EMPLOYEE;
		else return CUSTOMER;
	}
	public static final int VIEW_LESS=0,VIEW_MORE_LESS=1,CUSTOMER=2,EMPLOYEE=3,FIRST_REG=4;
	private Module<Customer> customer;
	private Module<EmployeeContract> employee;
	private Module<String> userName;
	private Module<String> password;
}